package com.txzt.gaea.business.device.service.impl;

import com.txzt.gaea.business.device.domain.model.DeviceInfo;
import com.txzt.gaea.business.device.domain.model.DeviceMaintain;
import com.txzt.gaea.business.device.service.IDeviceInfoService;
import com.txzt.gaea.business.device.service.IDeviceMaintainService;
import com.txzt.gaea.common.base.service.impl.BaseServiceImpl;
import com.txzt.gaea.common.enums.DeviceStatus;
import com.txzt.gaea.common.enums.IsDeleteEnum;
import com.txzt.gaea.common.enums.MaintainType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class DeviceMaintainServiceImpl extends BaseServiceImpl<DeviceMaintain> implements IDeviceMaintainService {

    @Autowired
    private IDeviceInfoService deviceInfoService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addRepairDeviceMaintain(DeviceMaintain deviceMaintain) {
        DeviceInfo deviceInfo = deviceInfoService.selectById(deviceMaintain.getDeviceId());
        if (deviceMaintain.getMaintainQty() > deviceInfo.getAvailableQty()) {
            return 99;
        }
        //更新库存信息
        DeviceInfo target = new DeviceInfo();
        target.setId(deviceInfo.getId());
        target.setAvailableQty(deviceInfo.getAvailableQty() - deviceMaintain.getMaintainQty());
        target.setRepairQty(deviceInfo.getRepairQty() + deviceMaintain.getMaintainQty());
        deviceInfoService.updateById(target);
        //新增设备维护
        deviceMaintain.setDeviceCode(deviceInfo.getDeviceCode());
        deviceMaintain.setDeviceName(deviceInfo.getDeviceName());
        deviceMaintain.setDeviceBrand(deviceInfo.getDeviceBrand());
        deviceMaintain.setDeviceModel(deviceInfo.getDeviceModel());
        deviceMaintain.setDeviceType(deviceInfo.getDeviceType());
        deviceMaintain.setMaintainType(MaintainType.REPAIR.getCode());
        deviceMaintain.setDeviceStatus(DeviceStatus.REPAIR.getCode());
        deviceMaintain.setIsDelete(IsDeleteEnum.NOTDELETE.getCode());
        return sqlManager.insertTemplate(deviceMaintain);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addScrapDeviceMaintain(DeviceMaintain deviceMaintain) {
        DeviceInfo deviceInfo = deviceInfoService.selectById(deviceMaintain.getDeviceId());
        if (deviceMaintain.getMaintainQty() > deviceInfo.getAvailableQty()) {
            return 99;
        }
        //更新库存信息
        DeviceInfo target = new DeviceInfo();
        target.setId(deviceInfo.getId());
        target.setAvailableQty(deviceInfo.getAvailableQty() - deviceMaintain.getMaintainQty());
        target.setScrapQty(deviceInfo.getScrapQty() + deviceMaintain.getMaintainQty());
        deviceInfoService.updateById(target);
        //新增设备维护
        deviceMaintain.setDeviceCode(deviceInfo.getDeviceCode());
        deviceMaintain.setDeviceName(deviceInfo.getDeviceName());
        deviceMaintain.setDeviceBrand(deviceInfo.getDeviceBrand());
        deviceMaintain.setDeviceModel(deviceInfo.getDeviceModel());
        deviceMaintain.setDeviceType(deviceInfo.getDeviceType());
        deviceMaintain.setMaintainType(MaintainType.SCRAP.getCode());
        deviceMaintain.setDeviceStatus(DeviceStatus.SCRAP.getCode());
        deviceMaintain.setIsDelete(IsDeleteEnum.NOTDELETE.getCode());
        return sqlManager.insertTemplate(deviceMaintain);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteDeviceMaintain(Long id) {
        DeviceMaintain resMaintain = sqlManager.single(DeviceMaintain.class,id);
        DeviceInfo deviceInfo = deviceInfoService.selectById(resMaintain.getDeviceId());
        //更新库存信息
        DeviceInfo target = new DeviceInfo();
        target.setId(deviceInfo.getId());
        target.setAvailableQty(deviceInfo.getAvailableQty() + resMaintain.getMaintainQty());
        if (MaintainType.REPAIR.getCode().equals(resMaintain.getMaintainType())) {
            target.setRepairQty(deviceInfo.getRepairQty() - resMaintain.getMaintainQty());
        }else {
            target.setScrapQty(deviceInfo.getScrapQty() - resMaintain.getMaintainQty());
        }
        return deviceInfoService.updateById(target);
    }
}
